﻿Public Class FrmCentroCosto

    Private result As GBDZafiro.Resultado

    Private Sub FrmCentroCosto_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargarGv()
    End Sub

    Protected Overrides Sub bnGuardar_Click(sender As Object, e As System.EventArgs)
        If Not Validaciones.camposVacios(pBase) Then
            If guardar() Then
                MyBase.bnGuardar_Click(sender, e)
                cargarGv()
            End If
        End If
    End Sub

    Protected Overrides Sub bnEditar_Click(sender As Object, e As System.EventArgs)
        If tbCodigo.TextLength < 1 Then
            msnBox.msnExclamacion("No ha seleccionado un centro de costos!!!")

        Else
            MyBase.bnEditar_Click(sender, e)
            tbCodigo.Enabled = False

        End If
    End Sub

    Protected Overrides Sub bnEliminar_Click(sender As Object, e As System.EventArgs)
        If Not Validaciones.camposVacios(pBase) Then
            If msnBox.msnQuestion("¿Desea borrar el centro de costos ?") = Windows.Forms.DialogResult.Yes Then
                result = GBDZafiro.CentroCosto.Eliminar(crearCtrCostos())

                If result.ExisteError Then
                    msnBox.msnError(result.Mensaje)
                Else
                    MyBase.bnEliminar_Click(sender, e)
                    cargarGv()
                End If
            End If
        End If
    End Sub

    Private Sub gvCtroCostos_CurrentCellChanged(sender As System.Object, e As System.EventArgs)
        Dim row As DataGridViewRow = gvCtroCostos.CurrentRow
        If row IsNot Nothing Then
            tbCodigo.Text = row.Cells("COD_CENTRO").Value
            tbCtrCostos.Text = row.Cells("DESCRIPCION").Value
        End If
    End Sub

    Private Function guardar() As Boolean
        Dim ctrCostos As GBDZafiro.CentroCosto = crearCtrCostos()
        If GBDZafiro.CentroCosto.Exist(ctrCostos) Then
            result = GBDZafiro.CentroCosto.Actualizar(ctrCostos)

        Else
            result = GBDZafiro.CentroCosto.Nuevo(ctrCostos)

        End If

        If result.ExisteError Then
            msnBox.msnError(result.Mensaje)
        End If

        Return Not result.ExisteError
    End Function

    Private Sub cargarGv()
        RemoveHandler gvCtroCostos.CurrentCellChanged, AddressOf gvCtroCostos_CurrentCellChanged
        gvCtroCostos.DataSource = Nothing
        gvCtroCostos.DataSource = GBDZafiro.CentroCosto.ConsultarAll
        With gvCtroCostos
            .Columns("COD_CENTRO").HeaderText = "Codigo"
            .Columns("DESCRIPCION").HeaderText = "Centro de Costos"
        End With
        AddHandler gvCtroCostos.CurrentCellChanged, AddressOf gvCtroCostos_CurrentCellChanged
    End Sub

    Private Function crearCtrCostos() As GBDZafiro.CentroCosto
        Dim ctrCostos As New GBDZafiro.CentroCosto
        ctrCostos.COD_CENTRO = tbCodigo.Text.ToUpper
        ctrCostos.DESCRIPCION = tbCtrCostos.Text.ToUpper
        Return ctrCostos
    End Function

End Class
